////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek, 2010.
// -------------------------------------------------------------------------
//  File name:   CryTCPServiceFactory.h
//  Version:     v1.00
//  Created:     29/03/2010 by Paul Mikell.
//  Description: ICryDRM interface definition
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////


#ifndef __CRYTCPSERVICEFACTORY_H__

#define __CRYTCPSERVICEFACTORY_H__

#pragma once


#define INVALID_SOCKET_INDEX -1


class CCryTCPServiceFactory
{
public:

	static const uint32		k_defaultMaxSockets = 10;

	// Description:
	//	 Default constructor for CCryTCPServiceFactory.
	/* ctor */				CCryTCPServiceFactory();

	// Description:
	//	 Destructor for CCryTCPServiceFactory.
	virtual /* dtor */		~CCryTCPServiceFactory();

	//////////////////////////////////////////////////////////
	// ICryTCPServiceFactory implementation
	//////////////////////////////////////////////////////////

	// Description:
	//	 Initialise the ICryTCPServiceFactory.
	// Arguments:
	//	 maxSockets - maximum number of sockets
	// Return:
	//	 Result code.
	virtual ECryLobbyError	Initialise(
								uint32					maxSockets );

	// Description:
	//	 Terminate the ICryTCPServiceFactory.
	// Return:
	//	 Result code.
	virtual ECryLobbyError	Terminate(
								bool					isDestructing );

	// Description:
	//	 Get an ICryTCPService.
	// Arguments:
	//	 pUrl - server url.
	//	 port - port
	// Return:
	//	 Pointer to ICryTCPService for given server & port.
	virtual ICryTCPServicePtr	GetTCPService(const char* pUrl, uint16 port);

	// Description:
	//	 Tick.
	// Arguments:
	//	 tv - time.
	virtual void				Tick(
									CTimeValue				tv );

	// Description:
	//	 Increment reference count.
	// Return:
	//	 Reference count.
	virtual int				AddRef();

	// Description:
	//	 Decrement reference count.
	// Return:
	//	 Reference count.
	virtual int				Release();

	//////////////////////////////////////////////////////////

	// Description:
	//	 Get number of free sockets.
	// Return:
	//	 Number of free sockets.
	uint32					NumFreeSockets();

	// Description:
	//	 Connect a socket.
	// Arguments:
	//	 addr - address.
	//	 sockIdx - index of socket.
	// Return:
	//	 Result code.
	virtual ESocketError	Connect(
								sockaddr_in&			addr,
								int32&					sockIdx );

	// Description:
	//	 Send data to a socket.
	// Arguments:
	//	 sockIdx - socket index.
	//	 pData - data,
	//	 dataLen - size of data
	// Return:
	//	 Result code.
	virtual ESocketError	Send(
								int32					sockIdx,
								const char*				pData,
								size_t					dataLen,
								size_t&					sent );


	// Description:
	//	 Receive data from a socket.
	// Arguments:
	//	 sockIdx - socket index.
	//	 pData - data,
	//	 dataLen - size of data
	//	 endOfStream - has the end of the stream been reached,
	// Return:
	//	 Result code.
	virtual ESocketError	Recv(
								int32					sockIdx,
								char*					pData,
								size_t&					dataLen,
								bool&					endOfStream );

	// Description:
	//	 Close a connection
	// Arguments:
	//	 sockIdx - index of socket
	// Return:
	//	 Result code.
	virtual ESocketError	CloseConnection(
								int32&					sockIdx );

protected:

	// Description:
	//	 Create an ICryTCPService.
	// Arguments:
	//	 pUrl - server url.
	//	 port - port
	// Return:
	//	 Pointer to ICryTCPService for given server & port.
	virtual ICryTCPServicePtr	CreateTCPService(const char* pUrl, uint16 port);

private:

	// used to work out deltas from last tick
	CTimeValue				m_lastTickTimer;

	// Number of open sockets.
	uint32						m_numSockets;

	// Maximum number of sockets.
	uint32						m_maxSockets;

	// Sockets.
	SOCKET*						m_pSockets;

	// Services mutex.
	CryMutex						m_servicesMutex;

	// Services.
	std::vector<ICryTCPServicePtr>	m_services;

	// Reference count.
	volatile int			m_cnt;
};


typedef _smart_ptr<CCryTCPServiceFactory> CCryTCPServiceFactoryPtr;


#endif // __CRYTCPSERVICEFACTORY_H__
